//-------------------------------------------------------------------------------------

//              GHI Electronics, LLC

//               Copyright (c) 2010

//               All rights reserved

//-------------------------------------------------------------------------------------
/*
 * You can use this file if you agree to the following:
 *
 * 1. This header can't be changed under any condition.
 *    
 * 2. This is a free software and therefore is provided with NO warranty.
 * 
 * 3. Feel free to modify the code but we ask you to provide us with
 *	  any bugs reports so we can keep the code up to date.
 *
 * 4. This code may ONLY be used with GHI Electronics, LLC products.
 *
 * THIS SOFTWARE IS PROVIDED BY GHI ELECTRONICS, LLC ``AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL 
 * GHI ELECTRONICS, LLC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR ORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 *
 *	Specs are subject to change without any notice
 */

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace GHIElectronics.NETMF.FEZ
{
    public static partial class FEZ_Components
    {
        public class Button : IDisposable
        {
            InputPort button;

            public Button(FEZ_Pin.Digital pin)
            {
                button = new InputPort((Cpu.Pin)pin, false, Port.ResistorMode.PullUp);
            }

            public Button(FEZ_Pin.Interrupt pin) 
            {
                button = new InterruptPort((Cpu.Pin)pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
                button.OnInterrupt += new NativeEventHandler(OnInterrupt);
            }

            void OnInterrupt(uint data1, uint data2, DateTime time)
            {
                ButtonState state = (data2 == 0) ? ButtonState.Pressed : ButtonState.NotPressed;
                ButtonPressEvent((FEZ_Pin.Interrupt)data1, state);
            }

            public void Dispose()
            {
                button.Dispose();
            }

            public enum ButtonState : byte
            {
                NotPressed = 0,
                Pressed = 1,
            }

            public ButtonState GetState()
            {
                return (button.Read() == false) ? ButtonState.Pressed : ButtonState.NotPressed;
            }

            public delegate void ButtonPressEventHandler(FEZ_Pin.Interrupt pin, ButtonState state);
            public event ButtonPressEventHandler ButtonPressEvent = delegate { };

        }
    }
}

